package org.jiamandan.com.strategy.impl.tram.listener;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.dto.ZDBasicDto;
import org.jiamandan.com.strategy.impl.tram.IZDListenerService;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.ZDObjectUtil;
import org.springframework.stereotype.Service;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @see:
 * @创建日期：2022-12-12 14:00
 * @功能说明： JMD-YD获取时间

 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2022-12-01 16:50	        CWJ       	创建
 * @end
 */
@Service(value = BizConstant.CmdClass.CMD_YD_22_SERVICE)
@AllArgsConstructor
@Slf4j
public class YDDeviceTimeListenerService implements IZDListenerService {

    @Override
    public void doExecute(ZDBasicDto dto, Channel client) throws Exception {
        String deviceNo = StrUtil.toString(Integer.parseInt(CommonUtil.reverseHex(dto.getDeviceNo()).substring(2), 16));
        log.info("接收JMD-YD设备获取时间(CMD=22)【"+deviceNo+"】报文:" + JSON.toJSONString(dto));
        //时间戳
        String payload = CommonUtil.reverseHex(Long.toHexString(DateUtil.timer().interval() /1000));
        String replyMsg = ZDObjectUtil.ydpushMessage(payload, dto, BizConstant.Cmd.CMD_YD_22);
        log.info("应答JMD-YD设备获取时间(CMD=22)报文:" +replyMsg);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future= client.writeAndFlush(buf);
    }
}
